from django.http import HttpRequest
from django.test import Client, TestCase
import simcat.models
import simcat.views

__all__ = ["EntityTestCase", ]

class EntityTestCase(TestCase):
    fixtures = ['sampledata.json',]

    def setUp(self):
        self.c = Client()

    def testGet(self):
        response = self.c.get('/simcat/api/entity/1/')
        self.assertEqual(response.status_code, 200)
        self.assertNotEqual(response.content.find('telephone'),
                            -1)

    def testPost(self):
        e = simcat.models.Entity(name='Joe', email='joe@example.org')
        e.save()
        response = self.c.post('/simcat/api/entity/', e.to_json(),
                               content_type="application/json")
        self.assertEqual(response.status_code, 201)
        self.assertNotEqual(response.content.find('example.org'),
                            -1)

    def testCollectionRequest(self):
        request = HttpRequest()
        request.method = 'POST'
        request.raw_post_data = '{"name": "Rick"}'
        response = simcat.views.collection_request(request, 'entity')
        self.assertEqual(response.status_code, 201)
        self.assertNotEqual(response.content.find('telephone'),
                            -1)
